﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;

public partial class Users_CreateAncestralHall :BasePage
{
    protected string DefaultValue = "";
    private AncestralHallBLL bll = new AncestralHallBLL();
    private SaleNumber salBll = new SaleNumber();
    protected AspNetCs.Bll.Temple tempBll = new Temple();
    private int userid = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.aspx", "请您登录", true);
        userid = Base_GetUserInfo.UserId;
        if (!IsPostBack)
        {
            int Point = (int)Base_GetUserInfo.Point;
            if (Point < configInfo.ancestralhallPoint)
            {
                
                //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "您的可用点数:" + Point + ",创建宗祠需要" + inf.ancestralhallPoint+ "点,请您及时充值");
                ResultStr("Default.aspx", "您的可用点数:" + Point + ",创建宗祠需要" + configInfo.ancestralhallPoint + "点,请您及时充值");
            }
            ArrayList list = new ArrayList();
            DataTable dt = salBll.GetNetMuIdDataTable(" and SaleType=" + (int)Channel_Type.memorial + "  and  UserId=0");
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr["Number"]);
            }
            DefaultValue = bll.RndNum(list, RndLen);
            this.txtAncestralHallId.Text = DefaultValue;

        }

    }



    public AncestralHall_Model getModel()
    {
        Users_Model userModel = Base_GetUserInfo;

        AncestralHall_Model model = new AncestralHall_Model();
        string AncestralHallId = txtAncestralHallId.Text;
        string Name = txtName.Text;
        string PreName = txtPreName.Text;
        //int CreateId = 0;

        int Province = 0;
        int City = 0;
        if (Request["selProvice"] != null)
        {
            Province = StringHelper.isNumber_int(Request["selProvice"].ToString());

        }
        if (Province > 0)
        {
            if (Request["selCity"] != null)
            {
                City = StringHelper.isNumber_int(Request["selCity"].ToString());
            }
        }
        string Introduction = WebEditor1.Text;
        int MusicId = 0;
        int TempleId = StringHelper.isNumber_int(Request["templateId"]);
        DateTime CreateTime = DateTime.Now;
        int IsCheck = 0;
        int isDel = 0;
        int AcceeCount = 0;

        model.AncestralHallId = AncestralHallId;
        model.Name = txtName.Text;
        model.PreName = PreName;

        if (userModel.UserId > 0)
        {
            model.CreateId = userModel.UserId;
        }
        model.Province = Province;
        model.City = City;
        model.Introduction = Introduction;
        model.MusicId = MusicId;
        model.TempleId = TempleId;
        model.CreateTime = DateTime.Now;
        model.IsCheck = IsCheck;
        model.IsDel = isDel;
        model.AcceeCount = AcceeCount;

        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        int Id = 0;

        AncestralHall_Model model = getModel();
        Id=bll.Add(model);

        AncestralHallBBS_Model bbsModel = new AncestralHallBBS_Model();
        AncestralHallBBSBLL bbsBLL = new AncestralHallBBSBLL();
        bbsModel.AncestralHallId = Id;
        bbsModel.BBSTop = "";
        bbsModel.BBSBottom = "";
        bbsModel.BBSAnnouce = "";
        bbsModel.CreateTime = DateTime.Now;
        bbsModel.IsCheck = 0;
        bbsModel.IsDel = 0;
        bbsBLL.Add(bbsModel);

        AncestralHallItems_Model hallModel1 = new AncestralHallItems_Model();
        AncestralHallItems_Model hallModel2 = new AncestralHallItems_Model();
        AncestralHallItems_Model hallModel3 = new AncestralHallItems_Model();

        AncestralHallItemsBLL hallBLL = new AncestralHallItemsBLL();
        hallModel1.AncestralHallId = Id;
        hallModel1.ItemName = "宗祠规划";
        hallModel1.ItemLogo = "";
        hallModel1.Introduction = "";
        hallModel1.CreateTime = DateTime.Now;
        hallModel1.IsCheck = 0;
        hallModel1.IsDel = 0;
        hallModel1.IsSys = 1;

        hallModel2.AncestralHallId = Id;
        hallModel2.ItemName = "宗祠留言";
        hallModel2.ItemLogo = "";
        hallModel2.Introduction = "";
        hallModel2.CreateTime = DateTime.Now;
        hallModel2.IsCheck = 0;
        hallModel2.IsDel = 0;
        hallModel2.IsSys = 1;


        hallModel3.AncestralHallId = Id;
        hallModel3.ItemName =model.PreName +"姓族谱";
        hallModel3.ItemLogo = "";
        hallModel3.Introduction = "";
        hallModel3.CreateTime = DateTime.Now;
        hallModel3.IsCheck = 0;
        hallModel3.IsDel = 0;
        hallModel3.IsSys = 1;


        hallBLL.Add(hallModel1);
        hallBLL.Add(hallModel2);
        hallBLL.Add(hallModel3);

        DrePoint();

        ResultStr("AncestralHallList.aspx", "添加成功!");
    }

    /// <summary>
    /// 扣除点数
    /// </summary>
    protected void DrePoint()
    {
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        if (UserId != 0)
        {
            if ((TemplatePoint + configInfo.ancestralhallPoint) <= Base_GetUserInfo.Point)
            {
                //userBll.DrePoint(TemplatePoint, UserId);
                Temple_Model TmpMd = TempleBll.CreateNew.GetModel(TempId);
                Point_Log_Model poinLogModel = new Point_Log_Model();
                poinLogModel.NetIp = StringHelper.Ip();
                poinLogModel.Point = TemplatePoint + configInfo.ancestralhallPoint;
                if (TemplatePoint != 0)
                {
                    poinLogModel.Readme = "创建宗祠扣除" + configInfo.ancestralhallPoint + "点,选择模板["+TmpMd.TempleName+"]扣除" + TemplatePoint + "点";
                }
                else
                {
                    poinLogModel.Readme = "创建宗祠扣除" + configInfo.ancestralhallPoint + "点,选择了免费模板["+TmpMd.TempleName+"]";
                }
                
                poinLogModel.UserId = this.userid;
                poinLogModel.UserType =(int) UserTypeEnum.User;
                Point_Log_Bll.CreateNew.Add(poinLogModel);
            }
            else
            {
                Response.Write("<script>alert('无法创建,用户点数不足(需要" + (TemplatePoint + configInfo.ancestralhallPoint) + "点,您只有" + Base_GetUserInfo.Point + "点)!');location.href='CreateNetMu.aspx';</script>");
                return;
            }
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "请您登录", true);
        }
    }
}
